package com.nunbey.lingxi.activities;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.nunbey.lingxi.R;
import com.nunbey.lingxi.activities.sub.SubBaseActivity;
import com.nunbey.lingxi.apis.EcgApi;
import com.nunbey.lingxi.lincat.datastore.SharedPreferencesHelper;
import com.nunbey.lingxi.lincat.retrofitutil.RetrofitUtil;
import com.nunbey.lingxi.module.mode.record.EcgRecordReceive;
import com.nunbey.lingxi.module.mode.record.RecordListItem;

import org.json.JSONArray;
import org.json.JSONObject;

import java.time.LocalDateTime;
import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EcgRecordListActivity extends SubBaseActivity {

    private ArrayList<RecordListItem> recordListItems;

    private ArrayList<String> recordTimeList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ecg_record_list);
    }

    public static void startEcgRecordListActivity(Context context){
        context.startActivity(new Intent(context,EcgRecordListActivity.class));
    }

    @Override
    protected void onStart() {
        super.onStart();
        listViewClickInit();
        dataInit();
    }

    /**
     * 数据准备
     */
    private void dataInit(){
        EcgApi ecgApi = (EcgApi)RetrofitUtil.getApiInstance(EcgApi.class);
        SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper();
        String token = sharedPreferencesHelper.getData(this,"userInfo","token");
        ecgApi.getRecordList(token).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String resultJson = response.body().string();
                    JSONObject result = new JSONObject(resultJson);
                    int code = result.getInt("code");
                    if(code==0){
                        recordListItems = new ArrayList<>();
                        recordTimeList = new ArrayList<>();
                        JSONArray jsonArray = result.getJSONArray("data");
                        for(int i=0;i<jsonArray.length();i++){
                            JSONObject item = jsonArray.getJSONObject(i);
                            RecordListItem recordListItem = new RecordListItem(item.getLong("recordId"),item.getString("distinguish"),item.getString("recordTime"));
                            recordListItems.add(recordListItem);
                            recordTimeList.add(new String((i+1)+".记录时间："+item.getString("recordTime")));
                        }
                        flushList();
                    }else {
                        Toast.makeText(EcgRecordListActivity.this, "系统错误", Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void listViewClickInit(){
        ListView listView = findViewById(R.id.activity_ecg_record_list_listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Long recordId = recordListItems.get(i).getRecordId();
                Log.e("id",Long.toString(recordId));

                EcgApi ecgApi = (EcgApi)RetrofitUtil.getApiInstance(EcgApi.class);
                String token = new SharedPreferencesHelper().getData(EcgRecordListActivity.this,"userInfo","token");
                ecgApi.getRecordByRecordId(recordId,token).enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String resultJson = response.body().string();
                            JSONObject result = new JSONObject(resultJson);
                            int code = result.getInt("code");
                            if(code ==0){
                                JSONObject data = result.getJSONObject("data");
                                EcgRecordReceive recordReceive = new EcgRecordReceive(data.getLong("recordId"),data.getInt("averageRate"),data.getInt("heartRiskAwareness"),data.getInt("heartAge"),data.getInt("relaxation"),data.getInt("respiration"),data.getString("heartFitnessLevelLabel"),data.getInt("r_r_interval"),data.getString("distinguish"), LocalDateTime.now());
                                EcgRecordActivity.startEcgRecordActivity(EcgRecordListActivity.this,recordReceive);
                            }else
                                Toast.makeText(EcgRecordListActivity.this, "系统错误！", Toast.LENGTH_SHORT).show();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
            }
        });
    }

    private void flushList(){
        ArrayAdapter<String> arrayAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,recordTimeList);
        ListView listView = findViewById(R.id.activity_ecg_record_list_listview);
        listView.setAdapter(arrayAdapter);
    }


}